package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.FlowListDto;
import com.blueocean_health.care.management.domain.dto.UserAccountManagerDto;
import com.blueocean_health.care.management.domain.dto.UserListDto;
import com.blueocean_health.care.management.domain.dto.UserManagerListDto;
import com.blueocean_health.care.management.domain.vo.UserAccountManagerDvo;
import com.blueocean_health.care.management.domain.vo.UserListDvo;
import com.blueocean_health.care.management.service.UserCompanyService;
import com.github.pagehelper.PageInfo;

/**
 * 用户模块  2018年7月14日14:45:17
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("user/manager/")
public class UserCompanyController {
	
	@Resource
	private UserCompanyService userService;
	
	/**
	 * 用户管理授权联动 2018年7月17日18:10:17
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("authorize")
	public SysResult getTeamRoleList(){
		List<FlowListDto> typeList = userService.getTeamRoleList();
		return SysResult.ok(typeList);
	}
	/**
	 * 用户管理导出Excel 2018年7月17日18:10:17
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("list/excel")
	public void getUserExcel(HttpServletRequest request, HttpServletResponse response){
		UserListDvo userDvo = new UserListDvo();
		try {
			userDvo.setEnableStatus(request.getParameter("enableStatus"));
			userDvo.setUserName(request.getParameter("userName"));
			userDvo.setUserRealName(request.getParameter("userRealName"));
			userDvo.setUserSource(request.getParameter("userSource"));
			userDvo.setTeamId(request.getParameter("teamId"));
			List<UserListDto> userListExcel = userService.getUserListExcel(userDvo);
			String finalFileName = URLEncoder.encode("用户管理_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, userListExcel, UserListDto.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 新增用户  2018年4月18日11:04:38
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("list")
	public SysResult getUserList(@RequestBody UserListDvo userDvo){
		PageInfo<UserListDto> userManagerList = userService.getUserCompanyList(userDvo);
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> pageMap = new HashMap<>();
		resultMap.put("list", userManagerList.getList());
		pageMap.put("total", userManagerList.getTotal());
		resultMap.put("pageInfo", pageMap);
		
		return SysResult.ok(resultMap);
	}
	/**
	 * 新增用户  2018年4月18日11:04:38
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("add")
	public SysResult saveUser(@RequestBody UserAccountManagerDvo userDvo){
		String userPhone = userDvo.getUser().getUserPhone();
		if(StringUtils.isEmpty(userPhone)){
			return SysResult.error("手机号不能为空");
		}else{
			userPhone = userPhone.trim();
			if(userPhone.length() <= 12){
				Pattern pattern = Pattern.compile("[0-9]*"); 
				if(!pattern.matcher(userPhone).matches()){
					return SysResult.error("手机号不正确");
				}
				
			}else{
				return SysResult.error("手机号不正确");
			}
		}
		return userService.addUser(userDvo);
	}
	/**
	 * 编辑用户信息  2018年4月18日11:19:51
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("update")
	public SysResult updateUser(@RequestBody UserAccountManagerDvo userDvo){
		String userPhone = userDvo.getUser().getUserPhone();
		if(StringUtils.isEmpty(userPhone)){
			return SysResult.error("手机号不能为空");
		}else{
			userPhone = userPhone.trim();
			if(userPhone.length() <= 12){
				Pattern pattern = Pattern.compile("[0-9]*"); 
				if(!pattern.matcher(userPhone).matches()){
					return SysResult.error("手机号不正确");
				}
				
			}else{
				return SysResult.error("手机号不正确");
			}
		}
		return userService.updateUser(userDvo);
	}
	@RequestMapping("update/enable")
	public SysResult updateEnable(@RequestBody Map<String,String> map){
		int updateEnableStatus = userService.updateEnableStatus(map.get("userId"), map.get("enableStatus"));
		if(updateEnableStatus > 0){
			return SysResult.ok();
		}else{
			return SysResult.error();
		}
		
	}
	
	
	/**
	 * 通过用户Id查询用户信息  2018年4月18日11:19:51
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("query")
	public SysResult getUserInfo(@RequestBody Map<String,String> map){
		String userId = map.get("userId");
		UserAccountManagerDto userInfo = userService.getUserInfo(userId);
		return SysResult.ok(userInfo);
	}
	/**
	 * 负责人下拉列表 只有组织的用户  2018年7月12日15:18:06
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("person/list")
	public SysResult getManagerList(@RequestBody Map<String,String> map){
		
		List<UserManagerListDto> userList = userService.getUserList(map);
		
		return SysResult.ok(userList);
	}
	
	
}
